package com.atguigu.edustatistic.service.impl;


import com.atguigu.edustatistic.client.MemberClient;
import com.atguigu.edustatistic.entity.StatisticsDaily;
import com.atguigu.edustatistic.mapper.StatisticsDailyMapper;
import com.atguigu.edustatistic.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-09-18
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private MemberClient memberClient;
    @Override
    public void createStatisticsByDay(String day) {
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        int i = memberClient.countRegister(day);
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

        StatisticsDaily statisticsDaily=new StatisticsDaily();
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setRegisterNum(i);
        statisticsDaily.setCourseNum(courseNum);
        statisticsDaily.setLoginNum(loginNum);
        statisticsDaily.setVideoViewNum(videoViewNum);
        this.save(statisticsDaily);
    }

    @Override
    public Map<String, Object> showChart(String type, String start, String end) {
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.select(type,"date_calculated");
        wrapper.between("date_calculated",start,end);
        wrapper.orderByAsc("date_calculated");
        List<StatisticsDaily> list = baseMapper.selectList(wrapper);
        List<String> day=new ArrayList<>();
        List<Integer> count=new ArrayList<>();
        for(StatisticsDaily li:list){
            day.add(li.getDateCalculated());
            switch (type){
                case "login_num":
                    count.add(li.getLoginNum());
                    break;
                case "register_num":
                    count.add(li.getRegisterNum());
                    break;
                case "video_view_num":
                    count.add(li.getVideoViewNum());
                    break;
                case "course_num":
                    count.add(li.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("dataList",count);
        map.put("dateList",day);
        return map;
    }
}
